package com.dats.util;

import java.util.*;

/**
 * @author sig
 * @since 2016/9/25
 */
public class StringUtil {

    public static boolean isNullEmpty(String str) {
        return Objects.isNull(str) || str.trim().isEmpty();
    }

    public static List<Integer> str2IntList(String str) {
        List<Integer> list = null;

        if (!isNullEmpty(str)) {
            list = new LinkedList<>();
            String[] strs = str.split("\\D+");

            for (String tmp : strs) {

                if (isNullEmpty(tmp)) continue;
                list.add(Integer.parseInt(tmp));
            }
        }
        return list;
    }

    public static List<Long> str2LongList(String str) {
        List<Long> list = null;

        if (!isNullEmpty(str)) {
            list = new LinkedList<>();
            String[] strs = str.split("\\D+");

            for (String tmp : strs) {

                if (isNullEmpty(tmp)) continue;
                list.add(Long.parseLong(tmp));
            }
        }
        return list;
    }

    public static List<Float> str2FloatList(String str) {
        List<Float> list = null;

        if (!isNullEmpty(str)) {
            list = new LinkedList<>();
            String[] strs = str.split("\\D+");

            for (String tmp : strs) {

                if (isNullEmpty(tmp)) continue;
                list.add(Float.parseFloat(tmp));
            }
        }
        return list;
    }

    public static List<Double> str2DoubleList(String str) {
        List<Double> list = null;

        if (!isNullEmpty(str)) {
            list = new LinkedList<>();
            String[] strs = str.split("\\D+");

            for (String tmp : strs) {

                if (isNullEmpty(tmp)) continue;
                list.add(Double.parseDouble(str));
            }
        }
        return list;
    }

    public static Map<Integer, Integer> str2IntMap(String str) {
        Map<Integer, Integer> map = null;
        List<Integer> list = str2IntList(str);

        if (!CollectionUtil.isNullEmpty(list)) {

            if (0 != list.size() % 2) try {
                throw new IllegalArgumentException("参数的个数非偶数位");
            } catch (Exception e) {
                LogUtil.console.error(e.getMessage(), e);
                return null;
            }
            map = new HashMap<>();

            for (int pos = 0, size = list.size();
                 pos < size; pos += 2)
                map.put(list.get(pos), list.get(pos + 1));
        }
        return map;
    }
}
